package ilanguage.iltokens.operations.arimethic;

import i386CodeGenerator.RegisterAssigner;
import ilanguage.iltokens.IL_SSA;
import ilanguage.iltokens.operations.Operations;
import lexer.EntryType;

/**
 * The Class ToInt.
 */
public class ToInt extends CasterILToken {

	/**
	 * Instantiates a new to int.
	 * 
	 * @param firstToken
	 *            the first token
	 */
	public ToInt(final IL_SSA firstToken) {
		super(firstToken);
	}

	/**
	 * Es una variable o esta en la pila porque es double
	 */
	public String GenerateCode() {
		String codigo = "";
		String firstArg = this.getFirstArg().getResultRegister();

		codigo += "\t; Comienzo de conversion a Int\n";
		String regAux = RegisterAssigner.getInstance().getRegistro();

		if (firstArg != "FPU")
			codigo += "\t\tfld " + firstArg + "\n";
		
		codigo += "\t\tfistp varauxInt\n";
		codigo += "\t\tmov " + regAux + " , varauxInt\n";
		this.setResultRegister(regAux);
		codigo += "\t; Fin de conversion a Int\n\n";
		return codigo;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see ilanguage.iltokens.operations.OperationToken#getOperationName()
	 */
	@Override
	public String getOperationName() {
		return Operations.CAST_TOINT.toString();
	}

	@Override
	public EntryType solveTypes() {
		return EntryType.Casting(getFirstArg().solveTypes(), EntryType.INT);
	}
}